<template>
  <div :class="['breadcrumb-box mask-image', !globalStore.breadcrumbIcon && 'no-icon']">
    <el-breadcrumb :separator-icon="ArrowRight">
      <transition-group name="breadcrumb">
        <el-breadcrumb-item v-for="(item, index) in breadcrumbList" :key="item.path">
          <div class="el-breadcrumb__inner is-link" @click="onBreadcrumbClick(item, index)">
            <!-- <el-icon class="breadcrumb-icon" v-show="item.meta.icon && globalStore.breadcrumbIcon">
              <component :is="item.meta.icon"></component>
            </el-icon> -->
            <!-- {{ item.meta.icon }} -->
            <i
              :class="item.meta?.icon"
              class="iconfont2 breadcrumb-icon"
              v-show="item.meta?.icon && globalStore.breadcrumbIcon"
            ></i>
            <span class="breadcrumb-title">{{ item.meta ? item.meta?.title : item.MNU_Title }}</span>
          </div>
        </el-breadcrumb-item>
      </transition-group>
    </el-breadcrumb>
  </div>
</template>

<script setup lang="ts">
import { computed } from "vue";
import { HOME_URL, ABOUT_URL } from "@/config";
import { useRoute, useRouter } from "vue-router";
import { ArrowRight } from "@element-plus/icons-vue";
import { useAuthStore } from "@/stores/modules/auth";
import { useGlobalStore } from "@/stores/modules/global";
import { useDynamicStore } from "@/stores/modules/dynamic";
import { useFondsStore } from "@/stores/modules/fonds";
import localCache from "@/utils/cache";

const route = useRoute();
const router = useRouter();
const authStore = useAuthStore();
const globalStore = useGlobalStore();
const dynamicStore = useDynamicStore();
const fondsStore = useFondsStore();

const breadcrumbList = computed(() => {
  let breadcrumbData = authStore.breadcrumbListGet[route.matched[route.matched.length - 1].path] ?? [];
  console.log(1111111111111111, breadcrumbData);
  // return;

  // console.log(authStore.breadcrumbListGet, route.matched, breadcrumbData);

  // 🙅‍♀️不需要首页面包屑可删除以下判断
  if (breadcrumbData[0].path !== HOME_URL && breadcrumbData[0].path !== ABOUT_URL) {
    // console.log("breadcrumbData", breadcrumbData);

    if (breadcrumbData[0].name === "MainModal") {
      breadcrumbData[0].meta = localCache.getCache("fondObj", false) ? localCache.getCache("fondObj", false).meta : "";
    }
    if (breadcrumbData[1] && breadcrumbData[1].name === "sublevel") {
      const fondsData = fondsStore.fondsData.length > 0 ? fondsStore.fondsData : fondsStore.getFondsData;
      const filterData = fondsData.filter((v: any) => v.FMT_ID === route.query.fmtid && v.Archive_Status === route.query.status);
      breadcrumbData[1].meta.title = filterData[0].FMT_Name;
    }
    breadcrumbData = [{ path: HOME_URL, meta: { icon: "icon-shouye", title: "首页" } }, ...breadcrumbData];
  }
  return breadcrumbData;
});

// Click Breadcrumb
const onBreadcrumbClick = (item: Menu.MenuOptions, index: number) => {
  if (index !== breadcrumbList.value.length - 1) router.push(item.path);
};
</script>

<style scoped lang="scss">
.breadcrumb-box {
  display: flex;
  align-items: center;
  overflow: hidden;
  .el-breadcrumb {
    white-space: nowrap;
    .el-breadcrumb__item {
      position: relative;
      display: inline-block;
      float: none;
      .el-breadcrumb__inner {
        display: inline-flex;
        &.is-link {
          color: var(--el-header-text-color);
          &:hover {
            color: var(--el-color-primary);
          }
        }
        .breadcrumb-icon {
          margin-top: 2px;
          margin-right: 6px;
          font-size: 16px;
        }
        .breadcrumb-title {
          margin-top: 3px;
        }
      }
      &:last-child .el-breadcrumb__inner,
      &:last-child .el-breadcrumb__inner:hover {
        color: var(--el-header-text-color-regular);
      }
      :deep(.el-breadcrumb__separator) {
        position: relative;
        top: -1px;
      }
    }
  }
}
.no-icon {
  .el-breadcrumb {
    .el-breadcrumb__item {
      top: -2px;
      :deep(.el-breadcrumb__separator) {
        top: 2px;
      }
    }
  }
}
</style>
